<?php

require_once dirname(__FILE__).'/../lib/income_accountGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/income_accountGeneratorHelper.class.php';

/**
 * income_account actions.
 *
 * @package    sf_sandbox
 * @subpackage income_account
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class income_accountActions extends autoIncome_accountActions
{
  public function executeReport(sfWebRequest $request)
  {
    $this->forward404Unless($this->income_account = Doctrine::getTable('IncomeAccount')->find(array($request->getParameter('id'))), sprintf('Object income account does not exist (%s).', array($request->getParameter('id'))));

    $this->startdate=$request->getParameter('startdate');
    if(!$this->startdate)$this->startdate=MyDateTime::today()->getstartofmonth()->todate();
    $this->enddate=$request->getParameter('enddate');
    if(!$this->enddate)$this->enddate=MyDateTime::today()->getendofmonth()->todate();

    $query =  Doctrine::getTable('Transaction')
      ->createQuery('a')
      //->where('a.account_id is null')
      ->orderBy('a.date')
      ->where('a.income_account_id = '.$this->income_account->getId())
      ->andWhere('a.date >= "'.$this->startdate.'"')
      ->andWhere('a.date <= "'.$this->enddate.'"');

    $this->pager = new sfDoctrinePager('Transaction',100);//items per page
    $this->pager->setQuery($query);
    $this->pager->setPage($request->getParameter('page', 1));
    $this->pager->init();
    $this->transaction_list = $this->pager->getResults();

    //get initial balance 
    $initbalarray =  Doctrine::getTable('Transaction')
      ->createQuery('a')
      ->select('sum(a.income-a.expense) as balance')
      ->where('a.income_account_id = '.$this->income_account->getId())
      ->andWhere('a.date < "'.$this->startdate.'"')
      ->fetchArray();
      ;
    $this->initialbalance=$initbalarray[0]['balance'];

    $curbalarray =  Doctrine::getTable('Transaction')
      ->createQuery('a')
      ->select('sum(a.income-a.expense) as balance')
      ->where('a.income_account_id = '.$this->income_account->getId())
      ->fetchArray();
      ;
    $this->currentbalance=$curbalarray[0]['balance'];
  }
}
